<template>
  <el-dialog title="新增员工" :visible="dialogShow" @close="dialogFormVisible">
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="80px"
      style="width: 75%; margin: 0 auto"
    >
      <el-form-item label="姓名" prop="username">
        <el-input
          v-model="form.username"
          autocomplete="off"
          placeholder="请输入姓名"
        ></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input
          v-model="form.mobile"
          autocomplete="off"
          placeholder="请输入手机号"
        ></el-input>
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker
          type="date"
          placeholder="请选择入职时间"
          v-model="form.timeOfEntry"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="form.formOfEmployment" placeholder="请选择聘用形式">
          <el-option
            :label="item.value"
            :value="item.id"
            v-for="item in hireType"
            :key="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input
          v-model="form.workNumber"
          autocomplete="off"
          placeholder="请输入工号"
        ></el-input>
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input
          @blur="treeBlur"
          v-model="form.departmentName"
          autocomplete="off"
          placeholder="请选择部门"
          @focus="getTreeData"
        ></el-input>
        <el-tree
          v-if="showTree"
          default-expand-all
          :data="department"
          :props="defaultProps"
          v-loading="treeLoading"
          @node-click="nodeClick"
        ></el-tree>
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker
          type="date"
          placeholder="请选择转正日期"
          v-model="form.correctionTime"
        >
        </el-date-picker>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible">取 消</el-button>
      <el-button type="primary" @click="addstaff">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
// 导入接口
import { getDepartments } from "@/api/departments.js";
// 导入递归工具
import { transTotreeData } from "@/utils";
// 导入枚举库
import enumObj from "@/api/constant/employees.js";
// 表单确定点击事件增加员工
import { addstaffList } from "@/api/employees.js";
export default {
  props: {
    dialogShow: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      // 表单绑定数据
      form: {
        username: "",
        mobile: "",
        formOfEmployment: "",
        workNumber: "",
        departmentName: "",
        timeOfEntry: "",
        correctionTime: "",
      },
      //   表单规则
      rules: {
        username: [{ required: true, message: "不能为空!", trigger: "blur" }],
        mobile: [{ required: true, message: "不能为空!", trigger: "blur" }],
        formOfEmployment: [
          { required: true, message: "不能为空!", trigger: "blur" },
        ],
        workNumber: [{ required: true, message: "不能为空!", trigger: "blur" }],
        departmentName: [
          { required: true, message: "不能为空!", trigger: "change" },
        ],
        timeOfEntry: [
          { required: true, message: "不能为空!", trigger: "blur" },
        ],
        correctionTime: [
          { required: true, message: "不能为空!", trigger: "blur" },
        ],
      },
      //   树形控件绑定数据
      department: [],
      //   树形控件文字
      defaultProps: {
        label: "name",
        children: "children",
      },
      // 显示部门树列表
      showTree: false,
      //加载动画
      treeLoading: false,
      // 把枚举库中的对象赋值
      hireType: enumObj.hireType,
    };
  },
  methods: {
    //   取消对话框
    dialogFormVisible() {
      this.form = {
        username: "",
        mobile: "",
        formOfEmployment: "",
        workNumber: "",
        departmentName: "",
        timeOfEntry: "",
        correctionTime: "",
      };
      // 重置表单
      this.$refs.form.resetFields();
      // 隐藏对话框
      this.$emit("update:dialogShow", false);
    },
    //   部门列表获取焦点事件
    async getTreeData() {
      //   显示树形
      this.showTree = true;
      this.treeLoading = true;
      // // 加载部门数据
      let res = await getDepartments();
      // console.log(res);
      this.department = transTotreeData(res.data.depts, "");
      this.treeLoading = false;
    },
    // 树状图点击事件
    nodeClick(data, node, tree) {
      // data中包含了被点节点的数据赋值给表单的name显示
      this.form.departmentName = data.name;
      this.showTree = false;
    },
    // 隐藏树状图事件
    treeBlur(e) {
      if (e.relatedTarget.className.indexOf("el-tree-node") == -1) {
        this.showTree = false;
      }
    },
    // 确认新增员工
    addstaff() {
      this.$refs.form.validate(async (isok) => {
        if (isok) {
          await addstaffList(this.form);
          // 提示
          this.$message.success("新增员工成功!");
          // 隐藏对话框
          this.$emit("update:dialogShow", false);
          // 加载最后一页
          this.pageInfo = this.$parent.pageInfo;
          pageInfo.total += 1;
          let lastPage = Math.ceil(this.pageInfo.total / this.pageInfo.size);
          this.$parent.pageInfo.page = lastPage;
          // 重新加载数据
          this.$parent.getEmpList();
        }
      });
    },
  },
};
</script>

<style>
</style>